<?php

namespace app\util;

use Yii;

/**
 * Class Debug
 * @package app\debug
 */
class Debug
{
    protected static function debugForLog($filename, $content, $date = true, $dir = '', $append = false)
    {
        $path = Yii::$app->getBasePath() . '/debug/' . $dir;
        if (!file_exists($path)) {
            mkdir($path, 0777, true);
            chmod($path, 0777);
        }

        if ($append) {
            $date = false;
        }

        if ($date) {
            // win不支付特殊字符
            $filename .= '-' . date('YmdHis') . '.log';
        } else {
            $filename .= '.log';
        }

        $filename = $path . '/' . $filename;

        // todo file lock
        if (!$append) {
            file_put_contents($filename, $content);
        } else {
            file_put_contents($filename, $content . date('Y-m-d H:i:s') . PHP_EOL, FILE_APPEND);
        }
    }

    public static function wx($filename, $content, $date = true)
    {
        self::debugForLog($filename, $content, $date, 'wx');
    }

    public static function pay($filename, $content, $date = true)
    {
        self::debugForLog($filename, $content, $date, 'pay');
    }

    public static function commission($filename, $content, $date = true)
    {
        self::debugForLog($filename, $content, $date, 'commission');
    }

    public static function queue($filename, $content, $date = true, $append = true)
    {
        self::debugForLog($filename, $content, $date, 'queue', $append);
    }

    public static function debug($filename, $content, $date = true)
    {
        self::debugForLog($filename, $content, $date);
    }
}